
#include "colorselectbutton.h"
//#include "moc_colorselectbutton.cpp"	moc V5 sera incompatible V4

#include <QDebug>
#include <QColor>
#include <QMouseEvent>
#include <QPainter>
#include <QColorDialog>
#include <QPen>
#include <QBrush>
#include <QDesktopWidget>
#include <QApplication>

//---------------------------------------------------------------------------------
/**
 * @brief ColorSelectButton::ColorSelectButton
 * @param defaultColor
 * @param parent
 */
ColorSelectButton::ColorSelectButton (QWidget *parent):
    QWidget (parent),
    m_color (QColor (Qt::red))
{
    QDesktopWidget * dw = QApplication::desktop ();
    int buttonWidth = dw->screenGeometry ().width () / 68;	// taille de 20 px sur mon écran
    setFixedSize (QSize (buttonWidth, buttonWidth));
    setToolTip (tr ("Choisir une couleur"));
    setGraphicsEffect (blurEffect ());
}

ColorSelectButton::ColorSelectButton (const QColor& color, QWidget *parent):
    QWidget (parent),
    m_color (color)
{
    QDesktopWidget * dw = QApplication::desktop ();
    int buttonWidth = dw->screenGeometry ().width () / 68;	// taille de 20 px sur mon écran
    setFixedSize (QSize (buttonWidth, buttonWidth));
    setToolTip (tr ("Choisir une couleur"));
    setGraphicsEffect (blurEffect ());
}
//----------------------------------------------------------
/**
 * @brief ColorSelectButton::blurEffect
 * @return
 */
QGraphicsDropShadowEffect* ColorSelectButton::blurEffect ()
{
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect (this);
    effect->setBlurRadius (10.0);
    effect->setOffset (3.0);
    return effect;
}
//-------------------------------------
/**
 * @brief ColorSelectButton::~ColorSelectButton
 */
ColorSelectButton::~ColorSelectButton ()
{
}
//----------------------------------------------
/**
 * @brief ColorSelectButton::paintEvent
 */
void ColorSelectButton::paintEvent (QPaintEvent*)
{
    QPainter painter (this);
    redraw (painter);
}
//------------------------------------------------
/**
 * @brief ColorSelectButton::redraw
 * @param painter
 */
void ColorSelectButton::redraw (QPainter & painter)
{
    painter.setRenderHints (QPainter::Antialiasing, true);
    const QRectF& surface (rect ());
    painter.setPen (Qt::NoPen);

    QColor color (underMouse () ? m_color.lighter (150) : m_color); // bouton apparaît plus clair quand la souris le survole

    QRadialGradient gradientBrush (surface.center (), surface.width (), surface.topLeft ());
    gradientBrush.setColorAt (0.0, Qt::white);
    gradientBrush.setColorAt (0.5, color);
    gradientBrush.setColorAt (1.0, Qt::black);
    painter.setBrush (gradientBrush);

    painter.drawEllipse (surface);
}
//-----------------------------------------------------
/**
 * @brief ColorSelectButton::mousePressEvent
 */
void ColorSelectButton::mousePressEvent (QMouseEvent *)
{
    setGraphicsEffect (0);
}
//------------------------------------------------------------
/**
 * @brief ColorSelectButton::mouseReleaseEvent
 * @param event
 */
void ColorSelectButton::mouseReleaseEvent (QMouseEvent *event)
{
    setGraphicsEffect (blurEffect ());

    if (event->button () != Qt::LeftButton) return;
    event->accept ();

    QColorDialog cd (m_color, this);
    cd.setOption (QColorDialog::ShowAlphaChannel, false);
    cd.setWindowTitle (tr ("Choisissez une couleur"));

    int ret = cd.exec ();
    if (ret == QDialog::Accepted)
        setColor (cd.currentColor ());
}
//----------------------------------------------------
/**
 * @brief ColorSelectButton::setColor
 * @param color
 */
void ColorSelectButton::setColor (const QColor& color)
{
    if (m_color != color) {
        m_color = color;
        update ();
        emit colorChanged (color);
    }
}

